package org.example.com.controller;

import org.example.com.pojo.Result;
import org.example.com.pojo.ServiceReservation;
import org.example.com.pojo.vo.ServiceReservationVO;
import org.example.com.service.ServiceReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 服务预约Controller
 */
@RestController
@RequestMapping("/auth/service")
public class ServiceReservationController {
    
    @Autowired
    private ServiceReservationService serviceReservationService;
    
    /**
     * 提交服务预约
     * @param serviceReservation 服务预约信息
     * @return 预约ID
     */
    @PostMapping("/reservation")
    public Result submit(@RequestBody ServiceReservation serviceReservation) {
        try {
            int id = serviceReservationService.submit(serviceReservation);
            return Result.success(id);
        } catch (Exception e) {
            return Result.error("预约提交失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取用户服务预约历史
     * @return 预约列表
     */
    @GetMapping("/history")
    public Result getHistory() {
        try {
            List<ServiceReservationVO> history = serviceReservationService.getHistory();
            return Result.success(history);
        } catch (Exception e) {
            return Result.error("获取预约历史失败：" + e.getMessage());
        }
    }
    
    /**
     * 根据状态获取预约列表
     * @param status 预约状态
     * @return 预约列表
     */
    @GetMapping("/list")
    public Result getByStatus(@RequestParam(required = false) String status) {
        try {
            List<ServiceReservationVO> list;
            if (status != null && !status.isEmpty()) {
                list = serviceReservationService.getByStatus(status);
            } else {
                list = serviceReservationService.getHistory();
            }
            return Result.success(list);
        } catch (Exception e) {
            return Result.error("获取预约列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取预约详情
     * @param id 预约ID
     * @return 预约详情
     */
    @GetMapping("/detail/{id}")
    public Result getDetail(@PathVariable int id) {
        try {
            ServiceReservationVO detail = serviceReservationService.getDetail(id);
            if (detail == null) {
                return Result.error("预约不存在");
            }
            return Result.success(detail);
        } catch (Exception e) {
            return Result.error("获取预约详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新预约状态
     * @param id 预约ID
     * @param status 新状态
     * @return 操作结果
     */
    @PutMapping("/status/{id}")
    public Result updateStatus(@PathVariable int id, @RequestParam String status) {
        try {
            serviceReservationService.updateStatus(id, status);
            return Result.success();
        } catch (Exception e) {
            return Result.error("更新状态失败：" + e.getMessage());
        }
    }
    
    /**
     * 取消预约
     * @param id 预约ID
     * @return 操作结果
     */
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable int id) {
        try {
            serviceReservationService.cancel(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("取消预约失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除预约
     * @param id 预约ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable int id) {
        try {
            serviceReservationService.delete(id);
            return Result.success();
        } catch (Exception e) {
            return Result.error("删除预约失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取所有预约（管理员用）
     * @return 所有预约列表
     */
    @GetMapping("/admin/all")
    public Result getAll() {
        try {
            List<ServiceReservationVO> all = serviceReservationService.getAll();
            return Result.success(all);
        } catch (Exception e) {
            return Result.error("获取所有预约失败：" + e.getMessage());
        }
    }
}
